
#include "stdafx.h"
#include "main.h"

#include <qapplication.h>

#include <QSplashScreen>
QSplashScreen *splash;

#ifdef PF_SCRIPTING
#include <qsproject.h>
#endif

#include "pf_fontlist.h"
#include "pf_patternlist.h"

//delete by fengwei, no script module
//#include "PF_scriptlist.h"
#include "pf_settings.h"
#include "pf_system.h"
#include "pf_fileio.h"
#include "pf_filterfont.h"
#include "pf_filterdxf.h"
//#include "PF_filterdxf1.h"

#include "ui_dlginitial.h"

#include "ui_applicationwindow.h"

#ifndef QC_SPLASH_TXTCOL
# define QC_SPLASH_TXTCOL Qt::black
#endif

//// for image mime resources from png files
//extern void QINITIMAGES_LXCAD();

#ifdef PF_SCRIPTING
//	extern void qInitImages_LXCAD();
#endif

#ifdef QC_BUILTIN_STYLE
        extern void applyBuiltinStyle();
#endif



/**
 * Main. Creates Application window.
 *
 * Cleaning up #defines.
 */
int main(int argc, char** argv) {

    PF_DEBUG->setLevel(PF_Debug::D_WARNING);
        PF_DEBUG->print("param 0: %s", argv[0]);

        QCoreApplication::setApplicationName(XSTR(LXCAD));
        QCoreApplication::setApplicationVersion(XSTR(QC_VERSION));

    QApplication app(argc, argv);


        // for image mime resources from png files
        //  TODO: kinda dirty to call that explicitly
        //QINITIMAGES_LXCAD();
#ifdef PF_SCRIPTING
//	qInitImages_lxcad();
#endif

        for (int i=0; i<app.argc(); i++) {
                if (QString("--debug") == app.argv()[i]) {
                PF_DEBUG->setLevel(PF_Debug::D_DEBUGGING);
                }
        }

        QFileInfo prgInfo( QFile::decodeName(argv[0]) );
        QString prgDir(prgInfo.dirPath(true));
    PF_SETTINGS->init(XSTR(QC_COMPANYKEY), XSTR(QC_APPKEY));
    PF_SYSTEM->init(XSTR(LXCAD), XSTR(QC_VERSION), XSTR(QC_APPDIR), prgDir);

        PF_FILEIO->registerFilter(new PF_FilterFont());
        PF_FILEIO->registerFilter(new PF_FilterDXF());

        // parse command line arguments that might not need a launched program:
        QStringList fileList = handleArgs(argc, argv);

        PF_String lang;
        PF_String langCmd;
        PF_String unit;

        PF_SETTINGS->beginGroup("/Defaults");

    PF_SETTINGS->endGroup();

        // show initial config dialog:
        if (unit=="Invalid") {
                PF_DEBUG->print("main: show initial config dialog..");
                UI_DlgInitial di(NULL);
                di.setText("<font size=\"+1\"><b>Welcome to " XSTR(LXCAD) "</b></font>"
                "<br>"
                "Please choose the unit you want to use for new drawings and your "
                "preferred language.<br>"
                "You can changes these settings later in the "
                "Options Dialog of " XSTR(LXCAD) ".");
                QPixmap pxm(":/main/intro_lxcad.png");
                di.setPixmap(pxm);
                if (di.exec()) {
                        PF_SETTINGS->beginGroup("/Defaults");
                        unit = PF_SETTINGS->readEntry("/Unit", "None");
                PF_SETTINGS->endGroup();
                }
                PF_DEBUG->print("main: show initial config dialog: OK");
        }

#ifdef QSPLASHSCREEN_H
        PF_DEBUG->print("main: splashscreen..");

        QPixmap* pixmap = new QPixmap(":/main/splash_lxcad.png");
# endif

        PF_DEBUG->print("main: init fontlist..");
    PF_FONTLIST->init();
        PF_DEBUG->print("main: init fontlist: OK");

        PF_DEBUG->print("main: init patternlist..");
    PF_PATTERNLIST->init();
        PF_DEBUG->print("main: init patternlist: OK");

        PF_DEBUG->print("main: init scriptlist..");
	//delete by fengwei, no script module	
    //PF_SCRIPTLIST->init();
        PF_DEBUG->print("main: init scriptlist: OK");

        PF_DEBUG->print("main: loading translation..");
        PF_SETTINGS->beginGroup("/Appearance");
#ifdef QC_PREDEFINED_LOCALE
                lang = PF_SETTINGS->readEntry("/Language", "");
                if (lang.isEmpty()) {
                        lang=QC_PREDEFINED_LOCALE;
                        PF_SETTINGS->writeEntry("/Language", lang);
                }
                langCmd = PF_SETTINGS->readEntry("/LanguageCmd", "");
                if (langCmd.isEmpty()) {
                        langCmd=QC_PREDEFINED_LOCALE;
                        PF_SETTINGS->writeEntry("/LanguageCmd", langCmd);
                }
#else
    lang = PF_SETTINGS->readEntry("/Language", "en");
    langCmd = PF_SETTINGS->readEntry("/LanguageCmd", "en");
#endif
        PF_SETTINGS->endGroup();

        PF_SYSTEM->loadTranslation(lang, langCmd);
        PF_DEBUG->print("main: loading translation: OK");

#ifdef QSPLASHSCREEN_H
        splash = new QSplashScreen(*pixmap);
        splash->show();
        splash->message(QObject::tr("Loading.."),
                Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
        PF_DEBUG->print("main: splashscreen: OK");
#endif

    //QApplication::setStyle(new QWindowsStyle());
    //QApplication::setStyle(new QPlatinumStyle());

#ifdef QC_BUILTIN_STYLE //js:
        PF_DEBUG->print("main: applying built in style..");
        applyBuiltinStyle();
#endif

        PF_DEBUG->print("main: creating main window..");
    UI_ApplicationWindow * appWin = new UI_ApplicationWindow();
        PF_DEBUG->print("main: setting caption");
    appWin->setCaption(XSTR(LXCAD));
        PF_DEBUG->print("main: show main window");
    appWin->show();
        PF_DEBUG->print("main: set focus");
        appWin->setFocus();
        PF_DEBUG->print("main: creating main window: OK");

#ifdef QSPLASHSCREEN_H
        if (splash) {
                PF_DEBUG->print("main: updating splash..");
                splash->message(QObject::tr("Loading..."),
                        Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
                PF_DEBUG->print("main: processing events");
                qApp->processEvents();
                PF_DEBUG->print("main: updating splash: OK");
        }
#endif

        PF_DEBUG->print("main: loading files..");
        bool files_loaded = false;
        for (QStringList::Iterator it = fileList.begin(); it != fileList.end();
                ++it ) {

#ifdef QSPLASHSCREEN_H
                        if (splash) {
                                splash->message(QObject::tr("Loading File %1..")
                                        .arg(QDir::convertSeparators(*it)),
                                Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
                                qApp->processEvents();
                        }
#endif
                        appWin->slotFileOpen(*it, LX_Define::FormatUnknown);
                        files_loaded = true;
        }
        PF_DEBUG->print("main: loading files: OK");

        // Set LC_NUMERIC so that enetring numeric values uses . as teh decimal seperator
        setlocale(LC_NUMERIC, "C");

#ifdef QSPLASHSCREEN_H
# ifndef QC_DELAYED_SPLASH_SCREEN
        if (splash) {
                splash->finish(appWin);
                delete splash;
                splash = 0;
        }
# endif
        delete pixmap;
#endif

    //app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));

        PF_DEBUG->print("main: app.exec()");

        if (!files_loaded) {
                appWin->slotFileNew();
        }

        appWin->slotRunStartScript();

        int r = app.exec();

        PF_DEBUG->print("main: Temporary disabled  delete appWin");
        // delete appWin;

        PF_DEBUG->print("main: finished");

        return r;
}



/**
 * Handles command line arguments that might not require a GUI.
 *
 * @return list of files to load on startup.
 */
QStringList handleArgs(int argc, char** argv) {
        PF_DEBUG->print("main: handling args..");
        QStringList ret;

        bool doexit = false;
        QString machine;
        QString input;
        QString output;

        for (int i=1; i<argc; i++) {
                if (QString(argv[i]).startsWith("-")==false) {
                        QString fname = QDir::convertSeparators(
                                QFileInfo(QFile::decodeName(argv[i])).absFilePath() );
                        ret.append(fname);
                }
                else if (QString(argv[i])=="--exit") {
                        doexit = true;
                }
        }


        if (doexit) {
                exit(0);
        }

        PF_DEBUG->print("main: handling args: OK");
        return ret;
}

